const {
  baseUrl,
  getRequest,
  postRequest
} = require("../../../utils/request.js")
const {
  mock_flag,
  login_flag
} = require("../../../data/index.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 注册信息
    username: '',
    password: '',
    passwordRe: '',
    //手机号验证码
    phone: '',
    code: '',
    canSendCode: true,
    sendCodeText: '发送验证码',
    countdown: 60
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  /**
   * 跳转
   */
  //登录
  to_login() {
    wx.navigateTo({
      url: '/pages/user/login/login'
    });
  },
  /**
   * 手机号注册
   */
  //发送验证码
  sendCode() {
    //判断手机号是否为空
    const phone = this.data.phone
    if (phone == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'error',
        duration: 2000
      })
      return
    }
    // 使用正则表达式校验手机号
    const phoneRegex = /^1[3-9]\d{9}$/
    if (!phoneRegex.test(phone)) {
      wx.showToast({
        title: '手机号格式错误',
        icon: 'error',
        duration: 2000
      })
      return
    }
    // 初始设置状态
    this.setData({
      canSendCode: false,
      sendCodeText: `${this.data.countdown}s后重试`
    })
    //设置定时器开启倒计时
    const countdownInterval = setInterval(() => {
      //倒计时还未结束
      if (this.data.countdown > 1) {
        this.setData({
          countdown: this.data.countdown - 1,
          sendCodeText: `${this.data.countdown - 1}s后重试`
        })
      } else {
        //倒计时已经结束,清除之前设置的定时器，停止定时执行回调函数。 
        clearInterval(countdownInterval)
        this.setData({
          canSendCode: true,
          sendCodeText: '发送验证码',
          countdown: 60
        })
      }
    }, 1000)
    //发送验证码
    if (mock_flag) {
      wx.showToast({
        title: '验证码为1111',
        icon: 'success',
        duration: 2000
      })
    }
    if (!mock_flag) {
      this.http_user_sendMessage()()
    }
  },
  //注册
  submitRegisterCode() {
    //判断个人信息是否为空
    if (this.data.username == '' || this.data.password == '') {
      wx.showToast({
        title: '请填写相应信息',
        icon: 'error',
        duration: 2000
      })
      return
    }
    // 检查两次输入的密码是否一致
    if (this.data.password !== this.data.passwordRe) {
      wx.showToast({
        title: '两次输入的密码不一致',
        icon: 'none'
      });
      return;
    }
    //判断手机号验证码是否为空
    if (this.data.phone == '' || this.data.code == '') {
      wx.showToast({
        title: '请输入手机号和验证码',
        icon: 'error',
        duration: 2000
      })
      return
    }
    //注册
    if (mock_flag) {
      if (this.data.code != '1111') {
        wx.showToast({
          title: '验证码错误',
          icon: 'error',
          duration: 2000
        })
        return
      }
      //跳转到登录
      this.to_login()
    }
    if (!mock_flag) {
      this.http_user_registerCheckCode()
    }
  },
  /**
   * 网络请求
   */
  //发送验证码
  http_user_sendMessage() {
    getRequest(baseUrl + "/front/user/sendMessage", {
      phone: this.data.phone
    }).then(res => {
      if (res.code == 200) {
        wx.showToast({
          title: '验证码发送成功',
          icon: 'success',
          duration: 2000
        })
      } else {
        wx.showToast({
          title: '验证码发送失败',
          icon: 'error',
          duration: 2000
        })
      }
    })
  },
  //验证用户注册验证码
  http_user_registerCheckCode() {
    postRequest(baseUrl + "/front/user/registerCheckCode", {
      phone: this.data.phone,
      checkCode: this.data.code
    }).then(res => {
      if (res.code == 200) {
        //注册
        this.http_user_register()
      } else {
        wx.showToast({
          title: '登录失败，请检查手机号和验证码',
          icon: 'error',
          duration: 2000
        })
      }
    })
  },
  //注册用户
  http_user_register() {
    getRequest(baseUrl + "/front/user/register", {
      username: this.data.username,
      password: this.data.password,
      phone: this.data.phone,
    }).then(res => {
      //console.log(res.data)
      if (res.code == 200) {
        wx.showToast({
          title: '注册成功',
          icon: 'success',
          duration: 2000
        })
        //跳转到登录
        this.to_login()
      }
    })
  },
})